library(ggpubr)

p <- ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose")
p

# Add gird lines
p + grids(linetype = "dashed")

# Add border line
p + border("black")

# Change backgroud color
p + bgcolor("#BFD5E3") + border("#BFD5E3")

# Set title and axis label
p2 <- ggpar(p, 
            title = "Boxplot created by ggpubr",
            subtitle = "Length by dose",
            caption = "Source: ggpubr",
            xlab = "Dose (mg)",
            ylab = "Teeth length",
            legend.title = "Dose (mg)")
p2

# Set font of title and labels
ggpar(p2,
      font.title = c(14, "bold.italic", "red"),
      font.subtitle = c(10, "orange"),
      font.caption = c(10, "orange"),
      font.x = c(14, "blue"),
      font.y = c(14, "#993333"))

# font() has the same effect
p2 +
    font("title", size = 14, color = "red", face = "bold.italic") +
    font("subtitle", size = 10, color = "orange") +
    font("caption", size = 10, color = "orange") +
    font("xlab", size = 12, color = "blue") +
    font("ylab", size = 12, color = "#993333")

# Set font in one function
ggpar(p,
      title = "Plot of length\nby dose",
      xlab = "Dose (mg)",
      legend.title = "Dose (mg)",
      font.title = c(14, "bold.italic", "red"),
      font.x = c(14, "bold", "#2E9FDF"),
      font.y = c(14, "bold", "#E7B800"))

# Set legend position and color
ggpar(p,
      legend = "right", legend.title = "Dose (mg)") +
    font("legend.title", color = "blue", face = "bold") +
    font("legend.text", color = "red")

# Set color
# ggpar() has 'palette' parameters, means palatte
#   ColorBrewer package and ggsci package's palette can be used.
ggpar(p, palette = c("#00AFBB", "#E7B800", "#FC4E07"))

ggpar(p, palette = "Dark2")
ggpar(p, palette = "grey")
ggpar(p, palette = "npg")   #nature

# ggpubr::color_palette(), fill_palette()
p + color_palette("jco")
p + color_palette(c("#00AFBB", "#E7B800", "#FC4E07"))

# Color gradient
# ggpubr::gradient_color(), gradient_fill()
p3 <- ggscatter(mtcars, x = "wt", y = "mpg", color = "mpg", size = 2)
#change the gradient color, use one custom color
p3 + gradient_color("red")

#use two colors
p3 + gradient_color(c("blue", "red"))

#use two colors
p3 + gradient_color(c("blue", "red"))

#use the RColorBrewer palette
p3 + gradient_color("RdYlBu")

# Set axis's limits, scales
# change y axis limits
ggpar(p, ylim = c(0, 50))

#change y axis cale to log2
ggpar(p, yscale = "log2")

#format  axis scale
#format.scale=TRUE, means Y axis scale is updated
ggpar(p, yscale = "log2", format.scale = TRUE)

# yscale() has the same effect
p + yscale("log2", .format = TRUE)

# User-defined axis label and scale
# change the font of x and y axis texts
# rotate x and y texts
p +
    font("xy.text", size = 12, color = "blue", face = "bold") +
    rotate_x_text(45) + rotate_y_text(45)

# remove ticks and axis texts
p + rremove("ticks") +
    rremove("axis.text")

# Set theme
#   The default is theme_pubr()
#   Themes of ggthemes can be used.
library(ggthemes)
p + ggthemes::theme_economist()

# Delete ggplot component
#   ggpubr::rremove() can del the component
p <- ggboxplot(ToothGrowth, x = "dose", y = "len", ggtheme = theme_igray())
p
p + rremove("grid")
